package com.play.game.mydb;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.play.game.mydb.bean.Book;

import org.litepal.LitePal;
import org.litepal.crud.DataSupport;

import java.util.List;

/**
 * Created by 聪明一只哈 on 2018/4/22.
 * Time: 2018/4/22  17:08
 */

public class FirstActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
    }
    public void create(View v){
        LitePal.getDatabase();
    }
    public void add(View v){
        Book book = new Book();
        book.setName("hashiqi");
        book.setAuthor("ha");
        book.setPages(100);
        book.setPrice(120);
        book.save();

    }
    public void delete(View v){
        DataSupport.deleteAll(Book.class,"id=?","2");
    }
    public void update(View v){
        Book book = new Book();
        book.setPages(200);
        book.updateAll("id=?","2");

    }
    public void query(View v){
        List<Book> bookList = DataSupport.findAll(Book.class);
        for (Book book: bookList) {
            Log.i("querybookList", "query: "+book.getId()+book.getName()+book.getPages()+book.getPrice()+book.getAuthor());

            Toast.makeText(FirstActivity.this, ""+book.getId()+book.getName()+book.getPages()+book.getPrice()+book.getAuthor(), Toast.LENGTH_SHORT).show();
        }

    }
}
